package zy;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;

public class Translate extends JFrame {

	private JPanel contentPane;
	private JTextField input;
	private LinkedList<Record> content = new LinkedList<Record>();
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Translate  frame = new Translate ();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Translate () {
		Record r[] = new Record[4];
		r[0] = new Record("hello", "你好");
		r[1] = new Record("world", "世界");
		r[2] = new Record("English", "英语");
		r[3] = new Record("Speak", "说话");
		for (Record i : r)
			content.add(i);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 650, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JTextArea output = new JTextArea();
		output.setBounds(90, 150, 300, 200);
		contentPane.add(output);

		JButton search = new JButton("查找");
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = input.getText();
				
				String regex = "[a-zA-Z]+";
				if (str.matches(regex)) {
					Iterator<Record> it = content.iterator();
					Record re;
					boolean flag = true;
					while (it.hasNext()) {
						re = it.next();
						if (str.equals(re.English)) {
							flag = false;
							output.setText(re.Chinese);
						}
						if (flag) {
							output.setText("没找到！");
						}
					}
				} else if (str.matches("[\u4e00-\u9fa5]+")) {
					output.setText("是中文");
					Iterator<Record> it = content.iterator();
					Record re;
					boolean flag = true;
					while (it.hasNext()) {
						re = it.next();
						if (str.equals(re.Chinese)) {
							flag = false;
							output.setText(re.English);
						}
						if (flag) {
							output.setText("没找到！");
						}
					}

				} else {
					output.setText("请输入正确的格式！");
				}
			}
		});
		search.setBounds(400, 70, 100, 40);
		contentPane.add(search);
		input = new JTextField();
		input.setBounds(90, 70, 300,40);
		contentPane.add(input);
		input.setColumns(10);

	}
}